package com.abkj.platform.tail.service.impl;

import com.abkj.platform.base.dao.warp.MapperWrap;
import com.abkj.platform.base.dao.warp.SQLType;
import com.abkj.platform.base.util.PageResult;
import com.abkj.platform.tail.service.BaseService;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("baseService")
public class BaseServiceImpl implements BaseService {

	@Autowired
	private MapperWrap mapperWrap;

	public void deleteById(Class<?> cz, int id) {
		this.mapperWrap.deleteById(cz, id);
	}

	public <T> T loadObjectById(Class<T> cz, int id) {
		return this.mapperWrap.loadObjectById(cz, id);
	}

	public Integer insertObject(Object obj) {
		return this.mapperWrap.insertOjbect(obj);
	}

	public void updateObject(Object obj, String cols) {
		this.mapperWrap.updateObject(obj, cols);
	}

	public <T> PageResult<T> findByPage(Class<T> cz, String countsql,
			String sql, int currentPage, int pageSize) {
		List countMap = this.mapperWrap.findBySql(countsql);
		int total = Integer.parseInt(((Map) countMap.get(0)).get("count")
				.toString());
		PageResult result = new PageResult(pageSize, currentPage, total);
		int start = (result.getCurrentPage() - 1) * result.getPageSize();
		if (start < 0)
			start = 0;
		List list = this.mapperWrap.findObjectsBySql(cz, sql + " limit "
				+ start + "," + pageSize);
		result.setList(list);
		return result;
	}

	public PageResult<Map> findByPage(String countsql, String sql,
			int currentPage, int pageSize) {
		List countMap = this.mapperWrap.findBySql(countsql);
		int total = Integer.parseInt(((Map) countMap.get(0)).get("count")
				.toString());
		PageResult result = new PageResult(pageSize, currentPage, total);
		int start = (result.getCurrentPage() - 1) * result.getPageSize();
		if (start < 0)
			start = 0;
		List list = this.mapperWrap.findBySql(sql + " limit " + start + ","
				+ pageSize);
		result.setList(list);
		return result;
	}

	public <T> List<T> findAllObjects(Class<T> cz) {
		return this.mapperWrap.findAllObjects(cz);
	}

	public <T> List<T> findObjectsBySql(Class<T> cz, String sql) {
		return this.mapperWrap.findObjectsBySql(cz, sql);
	}

	public List<Map> findBySql(String sql) {
		return this.mapperWrap.findBySql(sql);
	}

	public void excuteBySql(SQLType type, String sql) {
		this.mapperWrap.excuteBySql(type, sql);
	}

	public void createTableIfNotExist(String tablename, String sql) {
		this.mapperWrap.createTableIfNotExist(tablename, sql);
	}
}